home *** CD-ROM | disk | FTP | other *** search
/ AGA Toolkit '97 / The AGA Toolkit '97.iso / text / misc / port / detectmode.s < prev    next >
Encoding:
Text File  |  1996-09-07  |  3.8 KB  |  215 lines

  1.  
  2. ; Automatic mode detection routine:
  3.  
  4. ; This routine was dropped from Port since it is very hard to determine the
  5. ; mode. The PC box characters fool it into thinking a PC file as an Amiga
  6. ; file, and the square bracket in ANSI CSI fools it into thinking the file as
  7. ; an SF7 file.
  8.  
  9.  
  10.  
  11. ; Constants:
  12.  
  13. MODE_AUTOMODE    =     6
  14.  
  15. ; In Parse() routine:
  16.  
  17.     cmp.b    #"a",(a0)
  18.     beq.w    AutoMode
  19.  
  20. AutoMode:
  21.     tst.b    (a5,Mode_NoBuffer-DT)    ; Mode_NoBuffer=0? means we have buffer
  22.     bne    Err_NeedBuffer        ; if not
  23.     move.b    #MODE_AUTOMODE,(a5,Mode-DT)
  24.     bra.b    NameParse
  25.  
  26. ; Code just before starting the conversion, buffer already loaded:
  27.  
  28.     cmp.b    #MODE_AUTOMODE,(a5,Mode-DT)
  29.     bne.b    ReadFinished1        ; Determine the real mode to use.
  30.     bsr    DetectMode
  31.  
  32. ReadFinished1:
  33.  
  34.  
  35. ; The routine itself, which should be called before starting conversion.
  36.  
  37. DetectMode:
  38.     move.l    #Text_Checking,d2    ; Tell that we're checking the mode.
  39.     bsr    PrintText
  40.  
  41.     moveq    #0,d2            ; Clear each mode counts.
  42.     moveq    #0,d3
  43.     moveq    #0,d4
  44.     move.l    (a5,SourcePointer-DT),a0
  45.     cmp.l    #10240,(a5,LeftBytes-DT)
  46.     bcs.b    .LessData        ; Less data to determine with, otherwise
  47.     move.l    #10240-1,d1        ; use 10KB.
  48.     bra.b    .NextChar
  49.  
  50. .LessData:                ; Since we don't have 10KB for
  51.     move.l    (a5,LeftBytes-DT),d1    ; comparing, take all we can get.
  52.     subq.l    #1,d1
  53.  
  54. .NextChar:
  55.     move.b    (a0)+,d0        ; Get a byte.
  56.     cmp.b    #"{",d0            ; Signs of possible SF7.
  57.     beq.b    .SF7
  58.     cmp.b    #"[",d0            ; Ä
  59.     beq.b    .SF7
  60.     cmp.b    #"|",d0            ; Ö
  61.     beq.b    .SF7
  62.     cmp.b    #"\\",d0
  63.     beq.b    .SF7
  64.     cmp.b    #"}",d0            ; Å
  65.     beq.b    .SF7
  66.     cmp.b    #"]",d0
  67.     beq.b    .SF7
  68.  
  69.     cmp.b    #"ä",d0            ; Signs of possible Amiga format.
  70.     beq.b    .Amy
  71.     cmp.b    #"Ä",d0
  72.     beq.b    .Amy
  73.     cmp.b    #"ö",d0
  74.     beq.b    .Amy
  75.     cmp.b    #"Ö",d0
  76.     beq.b    .Amy
  77.  
  78.     IFD    Code020            ; Check if the special character range
  79.     cmp2.b    .BoundPC(pc),d0        ; is used, since it's only used on PC.
  80.     bhi.b    .PC
  81.     ENDC
  82.  
  83.     IFND    Code020
  84.     cmp.b    #$80,d0
  85.     bcc.b    .CheckUpper
  86.     bra.b    .Outside
  87.  
  88. .CheckUpper:
  89.     cmp.b    #$90,d0
  90.     bls.b    .PC
  91.  
  92. .Outside:
  93.     ENDC
  94.  
  95.     cmp.b    #$94,d0
  96.     beq.b    .PC
  97.     cmp.b    #$99,d0
  98.     beq.b    .PC
  99.     cmp.b    #CR,d0            ; CR, can't be an Amiga format.
  100.     beq.b    .Foreign
  101.  
  102. .ContLoop:
  103.     dbf.b    d1,.NextChar        ; If bytes left to check, do so.
  104.  
  105.     cmp.w    d2,d3            ; xx0
  106.     beq.b    .SameWarn1
  107.     cmp.w    d2,d4            ; x0x
  108.     beq.b    .SameWarn1
  109.     cmp.w    d3,d4            ; 0xx
  110.     beq.b    .SameWarn2
  111.  
  112. .TellMode:
  113.     movem.l    d2-d4,-(a7)
  114.     move.l    #Text_Uses,d2        ; Tell what mode it uses.
  115.     bsr    PrintText
  116.     movem.l    (a7)+,d2-d4
  117.  
  118.     cmp.w    d2,d3            ; SF7 or Amy
  119.     bhi.b    .NotSF7
  120.  
  121. .NotAmy:
  122.     cmp.w    d2,d4            ; SF7 or PC
  123.     bhi.b    .MakePC
  124.     bra.b    .MakeSF7
  125.  
  126. .NotSF7:
  127.     cmp.w    d3,d4            ; Amy or PC
  128.     bhi.b    .MakePC
  129.  
  130. .MakeAmy:
  131.     move.b    #MODE_TONULL,(a5,Mode-DT)
  132.     move.l    #Table_Null,(a5,Table-DT)
  133.     move.l    #Text_NullMode,d2    ; Tell the user that.
  134.     bsr    PrintText
  135.     rts
  136.  
  137. .MakePC:
  138.     move.b    #MODE_FROMPC,(a5,Mode-DT)
  139.     move.l    #Table_FromPC,(a5,Table-DT)
  140.     move.l    #Text_PCMode,d2        ; And tell that.
  141.     bsr    PrintText
  142.     rts
  143.  
  144. .MakeSF7:
  145.     move.b    #MODE_FROMSF7,(a5,Mode-DT)
  146.     move.l    #Table_FromSF7,(a5,Table-DT)
  147.     move.l    #Text_SF7Mode,d2    ; Tell it.
  148.     bsr    PrintText
  149.     rts
  150.  
  151. .SameWarn1:
  152.     cmp.w    d3,d4            ; Could be either one.
  153.     beq.b    Err_SameProbability
  154.     bra.b    .TellMode
  155.  
  156. .SameWarn2:                ; Same as above.
  157.     cmp.w    d2,d3
  158.     beq.b    Err_SameProbability
  159.     bra.b    .TellMode
  160.  
  161. .SF7:
  162.     addq.w    #1,d2            ; One point to SF7.
  163.     bra.b    .ContLoop
  164.  
  165. .Amy:
  166.     addq.w    #1,d3            ; One to Amiga.
  167.     bra.b    .ContLoop
  168.  
  169. .PC:
  170.     addq.w    #1,d4            ; One point to PC.
  171.     bra.b    .ContLoop
  172.  
  173. .Foreign:
  174.     addq.w    #1,d2            ; One point to SF7 and PC.
  175.     addq.w    #1,d4
  176.     bra.b    .ContLoop
  177.  
  178.     IFD    Code020
  179.  
  180. .BoundPC:                ; The cmp2 bounds for characters that
  181.     dc.b    $7f            ; only PC uses.
  182.     dc.b    $91
  183.  
  184.     ENDC
  185.  
  186. ; Error routines:
  187.  
  188. Err_SameProbability:
  189.     move.l    #Text_SameProbability,d2
  190.     bra.b    ShowErr
  191.  
  192. Err_NeedBuffer:
  193.     move.l    #Text_NeedBuffer,d2
  194.     bra.b    ShowErr
  195.  
  196. ; Data section:
  197.  
  198. Text_SameProbability:
  199.     dc.b    "Can't decide between modes.",0
  200.  
  201. Text_Uses:
  202.     dc.b    "Type: ",0
  203.  
  204. Text_NullMode:
  205.     dc.b    "Amiga\c0K\n",0
  206.  
  207. Text_SF7Mode:
  208.     dc.b    "SF7\c0K\n",0
  209.  
  210. Text_PCMode:
  211.     dc.b    "PC\c0K\n",0
  212.  
  213. Text_NeedBuffer:
  214.     dc.b    "Automatic mode detection only works with buffered input.",0
  215.